﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Rison.Web.Common;
using Rison.BusinessFacade.Helper;
using Rison.BusinessFacade.Common;
using Rison.BusinessFacade.Entity.NRBZ;
using Rison.BusinessFacade.Facade.NRBZ;
using Rison.DataAccess.Common;

namespace Rison.Web.NRBZ
{
    public partial class GgfwDetail : NrfbPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public override void LoadInformation(DataRow dr)
        {
            if (dr != null)
            {
                rblIfbmtsxx.SelectedValue = dr["Ifbmtsxx"].ToString() == "是" ? "是" : "否";
                rblIfzhzxxx.SelectedValue = dr["Ifzhzxxx"].ToString() == "是" ? "是" : "否";
                rblIfbmshdt.SelectedValue = dr["Ifbmshdt"].ToString() == "是" ? "是" : "否";
                //rblIftsbmfw.SelectedValue = dr["Iftsbmfw"].ToString() == "是" ? "是" : "否";
                rblIfwjfwxx.SelectedValue = dr["Ifwjfwxx"].ToString() == "是" ? "是" : "否";
            }

            //htmlTable
            LoadHtmlTable(NrbzSubType.NrbzSubType51.ToString(), "GgfwDetail1_FWXM1", "tableFWXM", 3);
            LoadHtmlTable(NrbzSubType.NrbzSubType52.ToString(), "GgfwDetail1_WJFW1", "tableWJFW1", 2);
            LoadHtmlTable(NrbzSubType.NrbzSubType53.ToString(), "GgfwDetail1_WJFW2", "tableWJFW2", 2);
            LoadHtmlTable(NrbzSubType.NrbzSubType54.ToString(), "GgfwDetail1_WJFW3", "tableWJFW3", 2);
            LoadHtmlTable(NrbzSubType.NrbzSubType55.ToString(), "GgfwDetail1_WJFW4", "tableWJFW4", 2);
            LoadHtmlTable(NrbzSubType.NrbzSubType56.ToString(), "GgfwDetail1_WJFW5", "tableWJFW5", 2);
            LoadHtmlTable(NrbzSubType.NrbzSubType57.ToString(), "GgfwDetail1_WJFW6", "tableWJFW6", 2);
            LoadHtmlTable(NrbzSubType.NrbzSubType58.ToString(), "GgfwDetail1_WJFW7", "tableWJFW7", 2);
            NrbzFacade facade = new NrbzFacade();

            DataSet ds = facade.LoadAttachement(NrbzId, 11);
            DataList11.DataSource = ds.Tables[0];
            DataList11.DataBind();
        }

        public override long SaveInformation()
        {
            NrbzFacade facade = new NrbzFacade();
            List<DataField> l = new List<DataField>();

            int returnValue = 0;

            l.Add(new DataField("Ifbmtsxx", rblIfbmtsxx.SelectedValue));
            l.Add(new DataField("Ifzhzxxx", rblIfzhzxxx.SelectedValue));
            l.Add(new DataField("Ifbmshdt", rblIfbmshdt.SelectedValue));
            //l.Add(new DataField("Iftsbmfw", rblIftsbmfw.SelectedValue));
            l.Add(new DataField("Ifwjfwxx", rblIfwjfwxx.SelectedValue));

            returnValue = facade.updateValue(l, NrbzId);

            //htmlTable
            string returnSubValue = "";
            returnSubValue = SaveHtmlTabel("GgfwDetail1_FWXM1", NrbzSubType.NrbzSubType51.ToString(), 3);
            returnSubValue = SaveHtmlTabel("GgfwDetail1_WJFW1", NrbzSubType.NrbzSubType52.ToString(), 2);
            returnSubValue = SaveHtmlTabel("GgfwDetail1_WJFW2", NrbzSubType.NrbzSubType53.ToString(), 2);
            returnSubValue = SaveHtmlTabel("GgfwDetail1_WJFW3", NrbzSubType.NrbzSubType54.ToString(), 2);
            returnSubValue = SaveHtmlTabel("GgfwDetail1_WJFW4", NrbzSubType.NrbzSubType55.ToString(), 2);
            returnSubValue = SaveHtmlTabel("GgfwDetail1_WJFW5", NrbzSubType.NrbzSubType56.ToString(), 2);
            returnSubValue = SaveHtmlTabel("GgfwDetail1_WJFW6", NrbzSubType.NrbzSubType57.ToString(), 2);
            returnSubValue = SaveHtmlTabel("GgfwDetail1_WJFW7", NrbzSubType.NrbzSubType58.ToString(), 2);

            uploadFile();
            return returnValue;
        }

        #region "子网站 create/delete/update htmlTable"
        /// <summary>
        /// 
        /// </summary>
        /// <param name="facade"></param>
        /// <param name="field">ZfgkDetail1_input_5_1</param>
        /// <returns></returns>
        private string SaveHtmlTabel(string field, string Type, int fieldNum)
        {
            string info = HttpContext.Current.Request.Form[field];

            if (!string.IsNullOrEmpty(info))
            {
                NrbzFacade facade = new NrbzFacade();
                string msg = facade.SaveSubInfobyParentId(NrbzId, Type, fieldNum, info);
                return msg;
            }
            return "";

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="facade"></param>
        /// <param name="tablename">ZfgkDetail1_table5_1</param>
        /// <param name="fieldname">ZfgkDetail1_input_5_1</param>
        private void LoadHtmlTable(string Type, string fieldname, string tablename, int fieldNum)
        {
            NrbzFacade facade = new NrbzFacade();
            DataTable dt = facade.LoadSubInfobyParentId(NrbzId, Type, fieldNum);
            if (dt == null || dt.Rows.Count == 0)
            {
                DrawOneRow(tablename, fieldname, "", "", ""); //blank row                
            }
            else
            {

                foreach (DataRow dr in dt.Rows)
                {
                    if (fieldNum == 2)
                    {
                        DrawOneRow(tablename, fieldname, dr[0].ToString(), dr[1].ToString(), "");
                    }
                    else if (fieldNum == 3)
                    {
                        DrawOneRow(tablename, fieldname, dr[0].ToString(), dr[1].ToString(), dr[2].ToString());
                    }
                 
                }

            }
        }

        private void DrawOneRow(string t, string inputname, string p1, string p2, string p3)
        {
            if (t == "tableFWXM")
            {
                //DrawOneRow1(t, inputname, p1,p2,p3);
            }
            else
            {
                DrawOneRow2(t, inputname, p1, p2);
            }

        }

        private void DrawOneRow1(string t, string inputname, string p1, string p2, string p3)
        {

            HtmlTableRow tr = new HtmlTableRow();
            HtmlTableCell ce = new HtmlTableCell();

            ce.InnerHtml = "项目名称 <input type='text' name='" + inputname + "' value='" + p1 + "' style='width:150px;' class='Text'/>";
            tr.Cells.Add(ce);

            ce = new HtmlTableCell();
            ce.InnerHtml = "内容描述 <input type='text' name='" + inputname + "' value='" + p2 + "' style='width:350px;' class='Text'/>";
            tr.Cells.Add(ce);

            if (p3 == "") p3 = "http://";

            ce = new HtmlTableCell();
            ce.InnerHtml = "链接地址 <input type='text' name='" + inputname + "' value='" + p3 + "' style='width:150px;' class='Text' />";
            tr.Cells.Add(ce);

            ce = new HtmlTableCell();
            ce.InnerHtml = "<img alt='删除' src='../style/images/_-.gif' onclick='removeParentNode(this);' class='img' />";
            tr.Cells.Add(ce);

            ((HtmlTable)this.FindControl(t)).Rows.Add(tr);
        }

        private void DrawOneRow2(string t, string inputname, string p1, string p2)
        {

            HtmlTableRow tr = new HtmlTableRow();
            HtmlTableCell ce = new HtmlTableCell();

            ce.InnerHtml = "栏目名称 <input type='text' name='" + inputname + "' value='" + p1 + "' style='width:300px;' class='Text'/>";
            tr.Cells.Add(ce);

            ce = new HtmlTableCell();
            ce.InnerHtml = "信息名称 <input type='text' name='" + inputname + "' value='" + p2 + "' style='width:350px;' class='Text'/>";
            tr.Cells.Add(ce);

            ce = new HtmlTableCell();
            ce.InnerHtml = "<img alt='删除' src='../style/images/_-.gif' onclick='removeParentNode(this);' class='img'/>";
            tr.Cells.Add(ce);

            ((HtmlTable)this.FindControl(t)).Rows.Add(tr);
        }

      

        #endregion

        #region "upload"
        public string uploadFile()
        {
            string val = Request.Params["fileUploader11_hdnFileVal"];
            List<SysAttachment> _attachments = new List<SysAttachment>();
            int returnValue = -1;
            string del = "";

            NrbzFacade facade = new NrbzFacade();

            //附件10
            if (!string.IsNullOrEmpty(val))
            {
                _attachments = NewUpload(val);
                returnValue = facade.InsertAttachment(NrbzId, 11, _attachments);
            }

            del = Request.Params["fileUploader11_del"];
            if (!string.IsNullOrEmpty(del))
                returnValue = facade.DeleteAttachment(del);


            if (returnValue < 0)
            {
                return "文件上传成功，保存有误！";
            }

            return "";
        }
        #endregion
    }
}